
package com.zhu.storesmanager.network;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import org.codehaus.jackson.map.ObjectMapper;

import android.content.Context;

import com.zhu.storesmanager.models.ModelItem;
import com.zhu.storesmanager.models.SyncModel;
import com.zhu.storesmanager.utils.Actions;
import com.zhu.storesmanager.utils.DatabaseUtil;
import com.zhu.storesmanager.utils.Log;

public class HttpUtil {
	public static final boolean isSecure = true; //是否使用安全登录接口
	public static final boolean isTest = true; //是否测试接口
    public static final String DEFAULT_HOST = isTest ? "http://www.5ischool.cn:8001/Sync/" : "http://59.60.0.23/Sync/";
    public static String HOST = DEFAULT_HOST;
    //key
    public static String mPublicKey = null;
    public static String mModulus = null;
    public static String mDesKey = null;

    private static ObjectMapper mObjectMapper = null;

    public static ObjectMapper getObjectMapper(){
        if(mObjectMapper == null){
            mObjectMapper = new ObjectMapper();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            mObjectMapper.setDateFormat(formatter); 
        }
        return mObjectMapper;
    }

    /**
     * 获取登录url
     * @return url
     */
    public static String getLoginUrl(){
        return HOST + "Login";
    }

    /**
     * 获取安全数据url
     * @return url
     */
    public static String getSecureUrl(){
        return HOST + "Secure";
    }

    /**
     * 获取安全登录url
     * @return url
     */
    public static String getSecureLoginUrl(){
        return HOST + "SecureLogin";
    }

    /**
     * 获取开始同步url
     * @return url
     */
    public static String getSyncStartUrl(){
        return HOST + "Start";
    }

    /**
     * 获取型号列表url
     * @return url
     */
    public static String getModelsUrl(){
        return HOST + "Model";
    }

    /**
     * 获取促销员签入上报url
     * @return url
     */
    public static String getPromoterCheckUrl(){
        return HOST + "PromoterCheck";
    }

    /**
     * 获取促销员销量上报url
     * @return url
     */
    public static String getPromoterSalesUrl(){
        return HOST + "SecurePromoterSales";
    }

    /**
     * 获取促销员任务上报url
     * @return url
     */
    public static String getPromoterTaskUrl(){
        return HOST + "PromoterContent";
    }

    /**
     * 获取同步完成url
     * @return url
     */
    public static String getFinishSyncUrl(){
        return HOST + "Finish2";
    }

    /**
     * 获取网络时间url
     * @return url
     */
    public static String getNetworkTimeUrl(){
        return HOST + "Time";
    }

    /**
     * 获取APK升级url
     * @param uuid
     * @return url
     */
    public static String getApkUpdateUrl(String uuid){
        return HOST + "Upgrade/" + uuid;
    }

    /**
     * 获取数据词典url
     * @return url
     */
    public static String getDictsUrl(){
        return HOST + "Dict";
    }

    /**
     * 获取数据标签数据
     * @return url
     */
    public static String getProjectLabelUrl(){
        return HOST + "Project";
    }

    /**
     * 获取提醒数据url
     * @return url
     */
    public static String getRemindUrl(){
        return HOST + "Reminder";
    }

    /**
     * 获取拜访内容数据url
     * @return url
     */
    public static String getVisitTaskUrl(){
        return HOST + "Visit";
    }

    /**
     * 获取拜访任务上传数据url
     * @return url
     */
    public static String getPutVisitCheckUrl(){
        return HOST + "VisitCheck";
    }

    /**
     * 获取拜访内容上传数据url
     * @return url
     */
    public static String getPutVisitContentUrl(){
        return HOST + "SecureVisitContent";
    }

    /**
     * 获取管理员区域、分办、渠道数据url
     * @return url
     */
    public static String getRegionUrl(){
        return HOST + "QueryRegion";
    }

    /**
     * 获取城市数据url
     * @return url
     */
    public static String getCityUrl(){
        return HOST + "QueryCity";
    }

    /**
     * 获取门店数据url
     * @return url
     */
    public static String getCounterUrl(){
        return HOST + "QueryCounter";
    }

    /**
     * 获取门店查访历史数据url
     * @return url
     */
    public static String getVisitListUrl(){
        return HOST + "QueryVisit";
    }

    /**
     * 获取门店查访内容数据url
     * @return url
     */
    public static String getVisitContentUrl(){
        return HOST + "SecureQueryVisitContent";
    }

    /**
     * 获取登录post上报内容
     * @param username 用户名
     * @param password 密码
     * @param context 上下文
     */
    public static String getPutLoginContent(Context context, String username, String password){
        SyncModel.LoginRequest lq= new SyncModel.LoginRequest();
        lq.LoginName = username;
        lq.Password = password;
        lq.Version = Actions.getVersionName(context);
        lq.Imei = Actions.getDeviceId(context);
        lq.AppType = "android";
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(lq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getLoginPutContent=" + result);
        return result;
    }

    /**
     * 获取开始同步post内容
     * @param uuid 权限id
     */
    public static String getPutSyncstartContent(String uuid, boolean isFirst){
        SyncModel.StartRequest sq= new SyncModel.StartRequest();
        sq.Uuid = uuid;
        sq.First = isFirst ? 1 : 0;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutSyncstartContent=" + result);
        return result;
    }

    /**
     * 获取型号列表post内容
     * @param uuid 权限id
     */
    public static String getPutModelsContent(String uuid){
        SyncModel.ProductRequest pq= new SyncModel.ProductRequest();
        pq.Uuid = uuid;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(pq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutModelsContent=" + result);
        return result;
    }

    /**
     * 获取促销员签入内容
     * @param uuid 权限id
     */
    public static String getPutPromoterCheckContent(Context context, String uuid){
    	String result = null;
    	SyncModel.PromoterCheckRequest pcq= DatabaseUtil.getPromoterCheckData(context);
        if(pcq != null){
        	pcq.Uuid = uuid;
        	try {
                result = getObjectMapper().writeValueAsString(pcq);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        Log.d("getPutSyncstartContent=" + result);
        return result;
    }

    /**
     * 获取促销员上报型号销量内容
     * @param uuid 权限id
     * @param list 上报销量数据列表
     * @return result 上报的json格式内容
     */
    public static String getPutPromoterSalesContent(Context context, String uuid, List<ModelItem> list){
    	String result = null;
    	if(list.size() > 0){
    		SyncModel.PromoterSalesRequest pcq= DatabaseUtil.getPromoterSalesData(context, list);
            if(pcq != null){
            	pcq.Uuid = uuid;
            	try {
                    result = getObjectMapper().writeValueAsString(pcq);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
    	}
        Log.d("getPutPromoterSalesContent=" + result);
        return result;
    }
    /**
     * 获取促销员上报型号销量内容
     * @param uuid 权限id
     * @return result 上报的json格式内容
     */
    public static String getPutPromoterTasksContent(Context context, String uuid){
    	String result = null;
		SyncModel.PromoterContentRequest pcq = DatabaseUtil.getPromoterTaskData(context);
		if (pcq != null) {
			pcq.Uuid = uuid;
			try {
				result = getObjectMapper().writeValueAsString(pcq);
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
        Log.d("getPutPromoterTasksContent=" + result);
        return result;
    }

    /**
     * 获取同步完成内容
     * @param uuid 权限id
     * @param syncTime 同步开始时间
     * @param type 1所有数据 2提醒
     */
    public static String getPutFinishSyncContent(String uuid, Date syncTime, int type){
    	String result = null;
    	SyncModel.FinishRequest fq= new SyncModel.FinishRequest();
        fq.Uuid = uuid;
        fq.SyncTime = syncTime;//new Date();
        fq.Type = type;
        try {
            result = getObjectMapper().writeValueAsString(fq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutFinishSyncContent=" + result);
        return result;
    }

    /**
     * 获取请求网络时间内容
     * @param uuid 权限id
     * @return result post内容
     */
    public static String getRequstNetworkTimeContent(String uuid){
        String result = null;
        SyncModel.TimeRequest tq= new SyncModel.TimeRequest();
        tq.Uuid = uuid;
        try {
            result = getObjectMapper().writeValueAsString(tq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getRequstNetworkTimeContent=" + result);
        return result;
    }

    /**
     * 获取数据词典post内容
     * @param uuid 权限id
     */
    public static String getPutDictsContent(String uuid){
        SyncModel.DictRequest dq= new SyncModel.DictRequest();
        dq.Uuid = uuid;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(dq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutDictsContent=" + result);
        return result;
    }

    /**
     * 获取数据标签post内容
     * @param uuid 权限id
     */
    public static String getPutProjectsContent(String uuid){
        SyncModel.ProjectRequest dq= new SyncModel.ProjectRequest();
        dq.Uuid = uuid;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(dq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutProjectsContent=" + result);
        return result;
    }

    /**
     * 获取提醒数据post内容
     * @param uuid 权限id
     */
    public static String getPutRemindsContent(String uuid){
        SyncModel.ReminderRequest dq= new SyncModel.ReminderRequest();
        dq.Uuid = uuid;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(dq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutRemindsContent=" + result);
        return result;
    }

    /**
     * 获取拜访任务post内容
     * @param uuid 权限id
     */
    public static String getPutVisitTasksContent(String uuid){
        SyncModel.VisitRequest dq= new SyncModel.VisitRequest();
        dq.Uuid = uuid;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(dq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutVisitTasksContent=" + result);
        return result;
    }

    /**
     * 获取区域post内容
     * @param uuid 权限id
     */
    public static String getPutRegionContent(String uuid){
        SyncModel.QueryRegionRequest sq= new SyncModel.QueryRegionRequest();
        sq.Uuid = uuid;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutRegionContent=" + result);
        return result;
    }

    /**
     * 获取城市post内容
     * @param uuid 权限id
     * @param branchId 分区ID
     */
    public static String getPutCityContent(String uuid, int branchId){
        SyncModel.QueryCityRequest sq= new SyncModel.QueryCityRequest();
        sq.Uuid = uuid;
        sq.BranchId = branchId;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutCityContent=" + result);
        return result;
    }

    /**
     * 获取门店post内容
     * @param uuid 权限id
     * @param cityCode 城市代码
     * @param channel 渠道ID
     * @param searchName 搜索的名字
     * @param branchCode 分办代码
     */
    public static String getPutCounterContent(String uuid, String branchCode, String cityCode, String channel, String searchName){
        SyncModel.QueryCounterRequest sq= new SyncModel.QueryCounterRequest();
        sq.Uuid = uuid;
        sq.CityCode = cityCode;
        sq.Channel = channel;
        sq.CounterName = searchName;
        sq.BranchCode = branchCode;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutCounterContent=" + result);
        return result;
    }

    /**
     * 获取门店拜访历史记录post内容
     * @param uuid 权限id
     * @param storeId 门店代码
     */
    public static String getPutVisitListContent(String uuid, String storeId){
        SyncModel.QueryVisitRequest sq= new SyncModel.QueryVisitRequest();
        sq.Uuid = uuid;
        sq.CounterCode = storeId;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutVisitListContent=" + result);
        return result;
    }

    /**
     * 获取门店拜访拜访内容记录post内容
     * @param uuid 权限id
     * @param visitId 拜访ID
     */
    public static String getPutVisitContent(String uuid, int visitId){
        SyncModel.QueryVisitContentRequest sq= new SyncModel.QueryVisitContentRequest();
        sq.Uuid = uuid;
        sq.VisitId = visitId;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutVisitContent=" + result);
        return result;
    }

    /**
     * 获取安全post上报内容
     * @param username 用户名
     * @param context 上下文
     */
    public static String getPutSecureContent(Context context, String username){
        SyncModel.SecureRequest lq= new SyncModel.SecureRequest();
        lq.LoginName = username;
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(lq);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutSecureContent=" + result);
        return result;
    }

    /**
     * 获取销量上报内容
     */
    public static String getPutSalesContent(SyncModel.DesDataVisitContentSales sales){
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(sales);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutSalesContent=" + result);
        return result;
    }

    /**
     * 获取销量上报内容
     */
    public static String getPutPromoterSalesContent(List<SyncModel.PromoterSalesReqModel> modelList){
        String result = null;
        try {
            result = getObjectMapper().writeValueAsString(modelList);
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.d("getPutPromoterSalesContent=" + result);
        return result;
    }
}
